package cn.finder0519.simplemediaplayer;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import androidx.core.app.NotificationCompat;

public class NotificationHelper {

    public static final String CHANNEL_ID = "media_playback_channel";
    private static final int NOTIFICATION_ID = 1;

    public static Notification buildPlaybackNotification(Context context, boolean isPlaying, boolean isLooping) {
        createNotificationChannel(context);

        String mediaUriString = context.getSharedPreferences("app", Context.MODE_PRIVATE)
                .getString("currentMediaUri", null);

        Intent intent = new Intent(context, PlayerActivity.class);
        if (mediaUriString != null) {
            intent.setData(Uri.parse(mediaUriString));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent contentPendingIntent = PendingIntent.getActivity(
                context,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // 播放/暂停按钮
        int playPauseIcon = isPlaying ? R.drawable.ic_pause : R.drawable.ic_play;
        String playPauseTitle = isPlaying ? "Pause" : "Play";

        Intent actionIntent = new Intent(context, PlaybackService.class);
        actionIntent.setAction(isPlaying ? PlaybackService.ACTION_PAUSE : PlaybackService.ACTION_PLAY);

        PendingIntent actionPendingIntent = PendingIntent.getService(
                context,
                1,
                actionIntent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        NotificationCompat.Action playPauseAction = new NotificationCompat.Action.Builder(
                playPauseIcon,
                playPauseTitle,
                actionPendingIntent
        ).build();

        // 循环开关按钮
        int loopIcon = isLooping ? R.drawable.ic_loop : R.drawable.ic_loop_off;
        String loopTitle = isLooping ? "Loop on" : "Loop off";

        Intent loopIntent = new Intent(context, PlaybackService.class);
        loopIntent.setAction(PlaybackService.ACTION_TOGGLE_LOOP);

        PendingIntent loopPendingIntent = PendingIntent.getService(
                context,
                2,
                loopIntent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        NotificationCompat.Action loopAction = new NotificationCompat.Action.Builder(
                loopIcon,
                loopTitle,
                loopPendingIntent
        ).build();

        Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle(context.getString(R.string.playing_background))
                .setContentText(context.getString(R.string.click_to_return))
                .setSmallIcon(R.drawable.ic_play)
                .setContentIntent(contentPendingIntent)
                .setOngoing(true)
                .addAction(playPauseAction)
                .addAction(loopAction)
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                        .setShowActionsInCompactView(0, 1) // 第0个和第1个按钮放小布局
                )
                .build();

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(NOTIFICATION_ID, notification);

        return notification;
    }


    public static void cancelPlaybackNotification(Context context) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(NOTIFICATION_ID);
    }

    private static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    context.getString(R.string.notification),
                    NotificationManager.IMPORTANCE_LOW);
            channel.setDescription(context.getString(R.string.notification_description));
            NotificationManager manager = context.getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }
}
